{#

This file is part of the Sonata package.

(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

#}


{#

This code manage the one-to-many association field popup

#}

{% autoescape false %}

<script>

    <!-- edit one association -->

    // handle the add link
    var field_add_{{ field_element.id }} = function(event) {

        event.preventDefault();
        event.stopPropagation();

        var form = jQuery(this).closest('form');

        // the ajax post
        jQuery(form).ajaxSubmit({
            url: '{{ url('sonata_admin_append_form_element', {
                'code': admin.root.code,
                'elementId': field_element.id,
                'objectId': admin.root.subject.id,
                'uniqid': admin.root.uniqid
            }) }}',
            type: "POST",
            data: { _xml_http_request: true },
            success: function(html) {
                jQuery('#sonata-ba-field-container-{{ field_element.id }}')
                    .replaceWith(html) // replace the html
                    .trigger('sonata.add_element');
            }
        });

        return false;
    };

    var field_widget_{{ field_element.id }} = false;

    // this function initialize the popup
    // this can be only done this way has popup can be cascaded
    function start_field_retrieve_{{ field_element.id }}(event) {
        event.preventDefault();

        // remove the html event a
        var a = jQuery(event.target).closest('a');
        a.attr('onclick', '');

        // initialize component
        field_widget_{{ field_element.id }} = jQuery("#field_widget_{{ field_element.id }}");

        // add the jQuery event to the a element
        a.click(field_add_{{ field_element.id }});

        // trigger the event
        a.trigger('click');

        return false;
    }

    <!-- edit one association -->
    
</script>


{% endautoescape %}